<template>
  <div id="confirm-order-page">
    <div class="page-title" :style="{top:(this.getSafeArea().top-4)+'px'}">
      <div id="back-button" @click="$router.push('/')">
        <i href="javascript:void(0)" class="el-icon-arrow-left"></i>
      </div>
      <span>订单确认</span>
    </div>

    <div style="position: relative; overflow-y: auto;overflow-x: hidden;padding-bottom: 55px; background: #f1f1f1;"
         :style="{
      top:(this.getSafeArea().top-4+35)+'px',
      height:(this.getWinSize().height-this.getSafeArea().top-50)+'px'
    }">
      <div class="address-box" v-show="sendType!==2">

        <!--      <div class="title-bar">地址信息</div>-->
        <address-item v-if="isGet" :addressData="address" :show-type="1"/>
        <div class="bottom-bar"></div>
        <div class="address-box-mask" v-if="isGet && address && address.id>0 && sendType===1 && sendData.sendPrice===0">
          {{ sameCityErrorInfo }}
        </div>
      </div>

      <div class="shop-and-product-box">
        <div class="shop-info">
          <div class="shop-logo">
            <img :src="shop.logo"/>
          </div>
          <div class="shop-name" v-text="shop.name"></div>
        </div>
        <product-list-item :ref="'product-list-item-'+index" v-for="(product,index) in productList" :key="index" :show-type="1" :curr-product="{
          id:product.shopProductId,
          name:product.shopProductName,
          model:product.model,
          nums:product.nums,
          price:product.price,
          score:product.score,
          image:product.image?product.image:product.shopProductImage,
        }"
                           :only-read="($route.query.shopSkillId && $route.query.shopSkillId>0) || $route.query.type==='group'?1:0"
                           :choose-model-call-back="chooseModel"
                           :curr-index="index"
                           :change-call-back="function(nums) {
                             changeNums(index,nums)
                           }"/>
        <div class="choose-send" style="border-bottom: 1px solid #f1f1f1">
          <div class="left">配送方式</div>
          <div class="right">
            <div class="send-type" @click="chooseSendType">
              <span v-text="dict.OrderSendTypeArr[sendType]"></span>
              <i class="el-icon-arrow-right"></i>
            </div>
            <div class="send-time" @click="chooseSendTime" v-if="sendType===1 && address.id>0">
              <div class="same-city-send-info"
                   v-html="sameCitySendInfo?sameCitySendInfo+' 前送达':'<span>请选择配送时间</span>'"></div>
            </div>
            <div class="send-data" v-if="sendType===1 && address.id>0 && sendData.sendPrice>0">
              <div class="send-data-info">
                <!--                <span>
                                  时长: {{ sendData.time + '分钟' }}
                                </span>-->
                <span>
                  距离: {{ (sendData.sendDistance * 0.001).toFixed(2) + 'km' }}
                </span>
                <span>
                  费用: {{ ToMoney(sendData.sendPrice) }}
                </span>
              </div>
            </div>
          </div>
        </div>
        <div class="choose-send">
          <div class="left">是否带发票</div>
          <div class="right">
            <!--            <el-switch-->
            <!--              v-model="isInvoice"-->
            <!--              active-color="#13ce66"-->
            <!--              inactive-color="#CCCCCC">-->
            <!--            </el-switch>-->
            <div class="send-type" @click="chooseInvoiceInfo">
              <span v-text="invoiceInfo.name?invoiceInfo.name+'('+invoiceInfo.typeName+')':'-'"></span>
              <i class="el-icon-arrow-right"></i>
            </div>
          </div>
        </div>
        <div class="choose-send">
          <div class="left">附言</div>
          <div class="right">
            <el-input type="textarea" v-model="info" maxlength="100" placeholder="订单备注"></el-input>
          </div>
        </div>
        <div class="c"></div>
      </div>

      <div class="product-price-detail">
        <div class="title">
          <span>
            金额明细
          </span>
<!--          <div style="color:#d9620f;display: inline-block; float: right; font-size: 12px; padding-right: 10px; text-decoration: underline;" @click="showLevelInfo">-->
<!--            vip可以享受折扣,点击查看-->
<!--          </div>-->
        </div>
        <div class="item">
          <div class="left">商品金额:</div>
          <div class="right">
            <span v-text="ToMoney(productPrice)"></span>
          </div>
        </div>
        <div class="item" v-if="sendType!==2">
          <div class="left" v-if="sendType===0">物流配送费:
            <!--Vip免配送费-->
            <el-tag v-if="isOrderFreeSendPrice" type="success">Vip免配送费</el-tag>
            <!--店铺免配送费-->
            <div class="tip-box free-express-price" v-if="!isOrderFreeSendPrice && expressPrice===0">
              免配送费 {{ isOrderFreeSendPrice }}
            </div>
          </div>

          <div class="left" v-if="sendType===1">同城配送费:
            <!--店铺免配送费-->
            <!--            <div class="tip-box free-express-price" v-if="expressPrice===0">-->
            <!--              免配送费-->
            <!--            </div>-->
          </div>

          <!-- 配送费>0 & 非自提 -->
          <div class="right b red" v-if="expressPrice>0 && sendType===0" v-text="ToMoney(expressPrice)"></div>
          <!-- 同城配送费 -->
          <div class="right b red" v-if="sendType===1" v-text="ToMoney(expressPrice)"></div>
        </div>
        <div class="item" v-if="['group'].indexOf($route.query.type)===-1 && !$route.query.shopSkillId">
          <div class="left">优惠券:
            <div v-if="useAbleCouponList.length>0" class="tip-box coupon-use-box" v-text="'您有'+useAbleCouponList.length+'张优惠券'"
                 @click="chooseCoupon"></div>
          </div>
          <div class="right">
            <span v-if="currCoupon && currCoupon.reduceMoney>0" v-text="'-'+ToMoney(currCoupon.reduceMoney)" @click="chooseCoupon"></span>
            <span v-if="!currCoupon.id">-</span>
            <i v-if="currCoupon && currCoupon.reduceMoney>0" class="el-icon-arrow-right" @click="chooseCoupon"></i>
          </div>
        </div>
        <div class="item">
          <div class="left">店铺满减:
            <div class="tip-box coupon-use-box" v-text="'满'+currFullMinus.full+'减'+currFullMinus.minus"
                 v-if="currFullMinus.full>0"></div>
          </div>
          <div class="right">
            <span v-if="currFullMinus.minus>0" v-text="'-'+ToMoney(currFullMinus.minus)"></span>
            <span v-if="!currFullMinus.minus || currFullMinus.minus===0" v-text="ToMoney(currFullMinus.minus)"></span>
          </div>
        </div>
        <div class="item" v-if="accountLevel>0 && vipDiscountPrice>0">
          <div class="left">会员优惠:
            <el-tag type="success" v-text="dict.VipLevelList[accountLevel].name"></el-tag>
            <el-tag type="warning" v-text="'折扣: '+(((1-vipDiscount).toFixed(2)-0)*100).toFixed(0)+'%'" v-if="vipDiscount>0"></el-tag>
          </div>
          <div class="right">
            <span class="red" v-text="'-'+ToMoney(vipDiscountPrice)"></span>
          </div>
        </div>
        <div class="item">
          <div class="left">合计:</div>
          <div class="right">
            <span class="red" v-text="'￥'+ToMoney(price)"></span>
          </div>
        </div>
        <div class="item" v-if="productScore>0">
          <div class="left">所需积分:
            <span class="b red" v-text="productScore"></span>
          </div>
          <div class="right">
            <div class="tip-box account-score-box">
              当前账户<span v-text="accountScore"></span>
            </div>
          </div>
        </div>
        <!--      <div class="item">-->
        <!--        <div class="left">店铺特殊优惠: </div>-->
        <!--        <div class="right" v-text="ToMoney(currCoupon.price)"></div>-->
        <!--      </div>-->
      </div>

      <div class="pay-choose-box">
        <div class="title">支付方式</div>
        <div class="item" @click="payType=0">
          <div class="left">
            <div class="icon money-pay"></div>
            <span>余额支付</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===0?'on':''"></div>
          </div>
        </div>
<!--        <div class="item" @click="payType=1" v-if="!(($route.query.score && $route.query.score>0) || productScore>0)">
          <div class="left">
            <div class="icon wechat-pay"></div>
            <span>微信</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===1?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=2" v-if="!(($route.query.score && $route.query.score>0) || productScore>0)">
          <div class="left">
            <div class="icon ali-pay"></div>
            <span>支付宝</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===2?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=3"  v-if="!(($route.query.score && $route.query.score>0) || productScore>0)">
          <div class="left">
            <div class="icon enterprise-pay"></div>
            <span>对公支付</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===3?'on':''"></div>
          </div>
        </div>-->


      </div>

    </div>
    <div class="page-bottom" align="center">
      <el-button type="danger" size="medium" @click="submit"
                 :disabled="sendType===1 && address.id>0 && sendData.sendPrice===0">确认购买
      </el-button>
    </div>

    <level-info-dialog ref="level-info-dialog"/>
  </div>
</template>

<script>
import AddressItem from '../common/AddressItem'
import ProductListItem from '../common/ProductListItem'
import CartTools from '../tools/CartTools'
import LevelInfoDialog from '../common/LevelInfoDialog'

export default {
  name: 'ConfirmOrder',
  components: {LevelInfoDialog, ProductListItem, AddressItem},
  data: function () {
    return {
      isInvoice: true,
      invoiceInfo: {
        name: '',
        number: '',
        bankAccount: '',
        addressMobile: ''
      },
      info: '',
      payType: 0,
      sendType: 0,
      sameCitySendInfo: '',
      isGet: true,
      totalMoney: 0,
      accountMoney: -1,
      accountScore: -1,
      accountLevel: -1,
      productScore: -1,
      vipDiscount: 0,
      vipDiscountPrice: 0,
      isOrderFreeSendPrice: false,
      sameCityErrorInfo:'当前地址不在配送范围内',
      address: {
        id: 0,
        name: '',
        mobile: '',
        provice: '',
        city: '',
        county: '',
        location: '',
        address: '',
        isDefault: 0,
      },
      shop: {},
      productList: [],
      couponList: [],
      useAbleCouponList: [],
      productBuyNumsMap: {},
      productPrice: 0,
      expressPrice: 10,
      currCoupon: {},
      currFullMinus: {
        full: 0,
        minus: 0,
      },
      shopDiscountPrice: -15,
      price: 180,
      selectData: {},
      sendData: {
        sendPrice: 0,
        time: '',
        sendDistance: 0
      }
    }
  },
  mounted () {
    let $this = this
    $this.initData(function () {
      $this.getShopDetail()
      $this.getCouponList()
      setTimeout(function () {
        $this.getDefaultAddress()
        $this.getAccountMoney()
      }, 500)
    })

  },
  methods: {
    chooseModel: function (item,index) {
      let $this = this
      if ($this.$route.query.type && $this.$route.query.type !== 'one') {
        return
      }
      $this.MyRequest($this.ApiDoNameMember + 'ShopProduct/getDetail', {
        id: item.id,
        model: item.model,
      }, function (productDetail) {
        $this.bus.$emit('ProductChooseModelDialogCallBus', 'productDetail', productDetail, function (stock) {
          var currProduct=$this.productList[index]
          $this.$set($this.productList[index],'model',stock.model)
          $this.$set($this.productList[index],'price',stock.price)
          $this.$set($this.productList[index],'image',stock.image)
          $this.$set($this.productList[index],'id',stock.id)
          $this.$set($this.productList[index],'score',!stock.score?0:stock.score)
          $this.calcProductMoney()
          $this.quChongProductList()
        })
        $this.$forceUpdate()
      })
    },
    showLevelInfo:function () {
      this.bus.$emit('LevelInfoDialogCallBus',0)
    },
    // 选择发票信息
    chooseInvoiceInfo: function () {
      let $this = this
      $this.bus.$emit('ChooseInvoiceInfoDialogCallBus', $this.invoiceInfo, function (invoiceInfo) {
        $this.invoiceInfo = invoiceInfo
        $this.$forceUpdate()
      })
    },
    // 提交订单
    submit: function () {
      let $this = this
      // 1.非自提需要地址信息
      if ($this.sendType !== 2 && !$this.address.id) {
        return $this.successTip('请先选择 地址', 'warning')
      }
      // 2.同城配送需要配送时间
      if ($this.sendType === 1 && !$this.sameCitySendInfo) {
        return $this.successTip('请先选择 配送时间', 'warning')
      }
      // 3.商品购买数据
      var productBuyNumsList = []
      $this.productList.forEach(function (value, index) {
        productBuyNumsList.push({
          id: value.shopProductId,
          nums: value.nums,
          model: value.model,
        })
      })

      let form = {
        info: $this.info,
        isInvoice: $this.isInvoice ? 1 : 0,
        payType: $this.payType,
        shopId: $this.shop.id,
        sendType: $this.sendType,
        addressId: $this.address.id,
      }
      // 团购只需要一个商品信息,且数量必须为 1
      if ($this.$route.query.type === 'group') {
        var productBuyNumsMap = productBuyNumsList[0]
        productBuyNumsMap.nums = 1
        form.productBuyNumsMap = JSON.stringify(productBuyNumsMap)
        if ($this.$route.query.groupOrderNo) {
          form.groupOrderNo = $this.$route.query.groupOrderNo
        }
      } else {
        form.productBuyNumsList = JSON.stringify(productBuyNumsList)
      }
      // 采购订单,增加采购订单编号参数
      if ($this.$route.query.type === 'shopProductPurchase') {
        form.shopProductPurchaseOrderNo = $this.$route.query.orderNo
      }
      // 4.优惠券判断
      if ($this.currCoupon.id) {
        form.couponId = $this.currCoupon.id
      }
      // 5.同城配送-配送时间
      form.sendTime = $this.sameCitySendInfo
      if ($this.payType === 0 && $this.price > $this.accountMoney) {
        return $this.successTip('当前账户余额不足', 'warning')
      }
      // 6.发票信息
      form.invoiceInfo = ''
      if ($this.invoiceInfo.name) {
        form.invoiceInfo = JSON.stringify($this.invoiceInfo)
      }

      // 7.积分兑换
      if (this.$route.query.extendType === 'score') {
        if (this.accountScore < this.productScore) {
          return this.$message.error('当前账户积分不足')
        }
      }

      form.extendType = this.$route.query.extendType?this.$route.query.extendType:''

      var url = 'Order/submit'
      if ($this.$route.query.type === 'group') {
        url = 'Order/submitGroup'
      } else {
        if ($this.$route.query.shopSkillId > 0) {
          form.shopSkillId = $this.$route.query.shopSkillId
        }
      }

      $this.MyRequest($this.ApiDoNameMember + url, form, function (orderNo) {
        // 商品详情: 立即购买，拼团购买-不进行购物车删除操作
        let noClearTypeArr = ['group', 'one', 'shopProductPurchase']

        var tmpStockIdList=[]
        $this.productList.forEach(function (value, index) {
          tmpStockIdList.push(value.id)
        })

        if (noClearTypeArr.indexOf($this.$route.query.type) === -1) {
          $this.bus.$emit('CartUtilCallBus','deleteCartProductStockItem',{
            id:tmpStockIdList.join(','),
            nums:0,
          },function () {})
        }

        var tmpList = []
        $this.productList.forEach(function (value, index) {
          tmpList.push({
            id: value.id,
            nums: value.nums,
            shopProductName: value.shopProductName,
            model: value.model,
          })
        })
        var orderData = {
          productList: tmpList.slice(0, 3),
          price: $this.price,
        }
        $this.payRequest(orderData, orderNo)
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    payRequest: function (productData, orderNo) {
      let $this = this

      // 对公支付,跳转至打款信息页面
      if ($this.payType === 3) {
        localStorage.setItem("PaySuccessOrderData",JSON.stringify(productData))
        // 附加商品,价格信息至参数
        $this.$router.push({
          name: 'EnterprisePayInfo',
          query: {
            type: 'order',
            orderNo: orderNo,
          }
        })
        return
      }

      if ($this.payType === 0 || $this.payType === 1 || $this.payType === 2) {

        // 跳转到支付成功
        function goToSuccess () {
          localStorage.setItem("PaySuccessOrderData",JSON.stringify(productData))
          $this.$router.push({
            name: 'PaySuccess',
            query: {
              type: 'order',
              payType: 0,
              orderNo: orderNo,
            }
          })
        }

        if ($this.$route.query.type === 'group' && $this.payType === 0) {
          goToSuccess()
          return
        }

        $this.MyRequest($this.ApiDoNameMember + 'Order/pay', {
          orderNo: orderNo,
          payType: $this.payType
        }, function (data) {

          if ($this.payType === 0) {
            $this.$message.success('支付成功')
            goToSuccess()
          }

          // 微信支付
          if ($this.payType === 1) {

            $this.pay('wxpay',{
              appid: data.appid,
              prepayid: data.prepayid,
              partnerid: data.partnerid,
              noncestr: data.noncestr,
              timestamp: data.timestamp,
              package: data.package,
              sign: data.sign
            },function () {
              $this.$message.success('支付成功')
              $this.dialogVisible=false
              goToSuccess()
              $this.$forceUpdate()
            },function () {
              $this.$message.info('未完成支付')
              $this.$router.push({
                name: 'OrderDetail',
                query: {
                  orderNo: orderNo,
                }
              })
            })
            /* var wxPayPlus = api.require('wxPayPlus')
            wxPayPlus.payOrder({
              apiKey: data.appid,
              orderId: data.prepayid,
              mchId: data.partnerid,
              nonceStr: data.noncestr,
              timeStamp: data.timestamp,
              package: data.package,
              sign: data.sign
            }, function (ret, err) {
              if (ret.status) {
                goToSuccess()
              } else {
                $this.$message.info('用户取消支付')
                $this.$router.push({
                  name: 'OrderDetail',
                  query: {
                    orderNo: orderNo,
                  }
                })
              }
            }) */
          }

          // 支付宝支付
          if ($this.payType === 2) {
            $this.pay('alipay',data,function () {
              $this.$message.success('支付成功')
              $this.dialogVisible=false
              goToSuccess()
              $this.$forceUpdate()
            },function () {
              $this.$message.info('未完成支付')
              $this.$router.push({
                name: 'OrderDetail',
                query: {
                  orderNo: orderNo,
                }
              })
            })
            /* var aliPayPlus = api.require('aliPayPlus')
            aliPayPlus.payOrder({
              orderInfo: data
            }, function (ret, err) {
              $this.$confirm('您是否支付完成?', '系统提示', {
                confirmButtonText: '已经支付',
                cancelButtonText: '返回订单',
                type: 'warning'
              }).then(() => {
                $this.$router.push({
                  name: 'OrderDetail',
                  query: {
                    orderNo: orderNo,
                  }
                })
              }).catch(() => {
                $this.$router.push({
                  name: 'OrderDetail',
                  query: {
                    orderNo: orderNo,
                  }
                })
              })
            }) */
          }

        })
      }
    },
    // 初始化信息
    initData: function (callback) {
      var selectProductDataString = localStorage.getItem('selectProductData')
      var selectData = JSON.parse(selectProductDataString)
      this.shop = selectData.shop
      this.productList = selectData.productList
      let $this = this
      callback()
      setTimeout(function () {
        $this.calcProductMoney()
      }, 1500)
    },
    calcProductMoney: function () {
      var productPrice = 0
      var productScore = 0
      let $this = this
      this.productList.forEach(function (product, index) {
        productScore=(productScore + ((product.nums * product.score).toFixed(2) - 0)).toFixed(2) - 0
        productPrice = (productPrice + ((product.nums * product.price).toFixed(2) - 0)).toFixed(2) - 0
      })
      $this.price = productPrice
      $this.productPrice = productPrice
      $this.productScore = productScore

      // ========= 运费计算 =========
      if (this.productPrice >= this.shop.freeExpressPrice) {
        this.expressPrice = 0
      }else{
        this.expressPrice = this.shop.expressPrice
      }
      if ($this.sendType === 1 && $this.sendData.sendPrice > 0) {
        $this.expressPrice = $this.sendData.sendPrice
      }
      // 会员-免运费
      if (this.accountLevel > 0 && this.sendType === 0) {
        if (dict.VipLevelList[$this.accountLevel].isOrderFreeSendPrice === 1) {
          $this.expressPrice = 0
          $this.isOrderFreeSendPrice = true
          $this.price = ($this.price - $this.expressPrice).toFixed(2) - 0
        } else {
          $this.isOrderFreeSendPrice = false
        }
      }
      var currFull = {
        full: 0,
        minus: 0,
      }
      this.shop.fullMinusPrice.forEach(function (item, index) {
        if (item.full <= $this.productPrice && item.minus > currFull.minus) {
          currFull = item
        }
      })
      $this.currFullMinus = currFull
      if ($this.sendType === 2) {
        $this.expressPrice = 0
      }
      if ($this.currFullMinus.minus > 0) {
        $this.price = (productPrice - $this.currFullMinus.minus).toFixed(2) - 0
      }
      if ($this.expressPrice > 0) {
        $this.price = ($this.price + $this.expressPrice).toFixed(2) - 0
      }
      if ($this.currCoupon.id > 0) {
        if ($this.productPrice<$this.currCoupon.fullMoney){
          // 优惠券，满足条件失效
          $this.currCoupon={
            id:0,
            fullMoney:0,
            reduceMoney:0,
          }
        }else{
          $this.price = ($this.price - $this.currCoupon.reduceMoney).toFixed(2) - 0
        }

      }

      // console.log('$this.productPrice', $this.productPrice)
      // console.log('$this.couponList', $this.couponList)
      // 筛选优惠券
      var tmpList = []
      $this.couponList.forEach(function (value, index) {
        if (value.fullMoney <= $this.productPrice) {
          tmpList.push(value)
        }
      })
      $this.useAbleCouponList = tmpList
      // console.log('$this.useAbleCouponList',$this.useAbleCouponList)

      // 积分兑换
      // if (this.$route.query && this.$route.query.extendType === 'score') {
      //   this.productScore = this.productList[0].nums * this.$route.query.score
      // }

      // 会员优惠金额(非团购,秒杀)
      if (['group'].indexOf($this.$route.query.type) === -1 && !$this.$route.query.shopSkillId) {
        if (this.accountLevel > 0) {
          this.vipDiscountPrice=0
          if (this.vipDiscount>0){
            this.vipDiscountPrice = ((1 - this.vipDiscount) * $this.productPrice).toFixed(2) - 0
          }
          this.price = (this.price - this.vipDiscountPrice).toFixed(2) - 0
        }
      }


      this.$forceUpdate()
    },
    // 商品列表去重
    quChongProductList: function () {
      let numsMap={}
      let $this=this
      $this.productList.forEach(function (productStock, index) {
        var key=productStock.id
        if (!numsMap[key]){
          numsMap[key]=0
        }
        numsMap[key]+=productStock.nums
      })

      var productIdModelArr=[]
      var newProductList=[]
      $this.productList.forEach(function (productStock, index) {
        var key=productStock.id
        if (productIdModelArr.indexOf(key)===-1){
          productIdModelArr.push(key)
          productStock.nums=numsMap[key]
          newProductList.push(productStock)
        }
      })

      console.log('newProductList',newProductList)
      console.log('numsMap',numsMap)

      $this.productList=newProductList
      $this.$forceUpdate()
    },
    // 获取-默认地址
    getDefaultAddress: function () {
      var $this = this
      var addressId = 0
      if ($this.$route.query.chooseAddressId) {
        addressId = $this.$route.query.chooseAddressId
      }
      $this.MyRequest($this.ApiDoNameMember + 'MemberAddress/getDetail', {id: addressId}, function (data) {
        if (data.id) {
          $this.address.id = data.id
          $this.address.name = data.name
          $this.address.province = data.province
          $this.address.city = data.city
          $this.address.county = data.county
          $this.address.mobile = data.mobile
          $this.address.address = data.address
          $this.address.isDefault = data.isDefault === 1
        }
        $this.isGet = true
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    // 更改数量回调
    changeNums: function (index,nums) {
      let $this=this
      $this.$set($this.productList[index],'nums',nums)
      this.calcProductMoney()
    },
    // 获取店铺信息
    getShopDetail: function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameMember + 'Shop/getDetail', {id: $this.shop.id}, function (data) {
        $this.shop = data
        $this.expressPrice = data.expressPrice
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    // 获取-当前账户最新余额
    getAccountMoney: function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameMember + 'Member/info', {}, function (data) {
        $this.accountMoney = data.money
        $this.accountScore = data.score
        if (data.isVip){
          $this.accountLevel = data.level
          $this.vipDiscount = data.vipDiscount
        }
        $this.calcProductMoney()
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    // 获取-优惠券
    getCouponList: function () {
      var productIdList=[]
      let $this = this
      $this.productList.forEach(function (value,index) {
        if (productIdList.indexOf(value.shopProductId)==-1){
          productIdList.push(value.shopProductId)
        }
      })
      $this.MyRequest($this.ApiDoNameMember + 'Member/getCouponList', {
        shopId: $this.shop.id,
        productIdList: productIdList.join(','),
        status: 1
      }, function (data) {
        $this.couponList = data.list
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    },
    chooseCoupon: function () {
      let $this = this
      console.log('this.useAbleCouponList',this.useAbleCouponList)
      if (!this.useAbleCouponList || this.useAbleCouponList.length === 0) {
        return
      }
      $this.bus.$emit('ChooseCouponDialogCallBus', this.productPrice, this.useAbleCouponList, function (coupon) {
        $this.currCoupon = coupon
        $this.calcProductMoney()
      },$this.currCoupon)
    },
    chooseSendType: function () {
      let $this = this
      $this.bus.$emit('ChooseSendTypeDialogCallBus', this.sendType, function (sendType) {
        $this.sendType = sendType
        if (sendType === 1) {
          $this.checkSameCitySend()
        } else {
          $this.sendData = {
            sendPrice: 0,
            sendDistance: 0,
            time: '',
          }
          $this.calcProductMoney()
        }
        $this.$forceUpdate()
      })
    },
    checkSameCitySend: function () {
      // console.log('checkSameCitySend')
      let $this = this
      // console.log('$this.addressId',$this.address.id)
      if (!$this.address.id) {
        return
      }
      $this.MyRequest($this.ApiDoNameMember + 'Order/getSendDataByAddress', {
        shopId: $this.shop.id,
        memberAddressId: $this.address.id
      }, function (sendData) {
        $this.sendData = sendData
        $this.calcProductMoney()
        // console.log('sendData',sendData)
        $this.$forceUpdate()
      }, function (res) {
        $this.sendData = {
          sendPrice: 0,
          distance: 0,
          time: '',
        }
        $this.sameCityErrorInfo=res.message
        $this.$forceUpdate()
        // $this.$message.error(msg)
      })
    },
    chooseSendTime: function () {
      let $this = this
      if (!$this.address || !$this.address.id) {
        return $this.successTip('请先选择地址')
      }
      if ($this.sendType === 1 && $this.sendData.sendPrice === 0) {
        return $this.$message.warning('当前地址不在配送范围内')
      }
      $this.bus.$emit('ShopSendTimeDialogCallBus', this.shop.id, this.address.id, function (sameCitySendInfo) {
        $this.sameCitySendInfo = sameCitySendInfo
        $this.$forceUpdate()
      })
    }
  }
}
</script>

<style scoped>

</style>
